This tutorial has everything about python keywords and identifiers, which is essential to every programming language.
Let’s start with What is Python Keyword?
Python Keywords
Every programming language has some reserved words with special meanings, and you can not use those names randomly. Those words are known as Keywords.
In Python 3 versions, there is a total of 32 reserved keywords. So, whenever you write a variable, ensure it's not a reserved keyword, or else the python interpreter will give an error.
For example:
If
is a reserved keyword means we can not use it as a variable or identifier.
>>> if = 34
File "<stdin>", line 1
if = 34
^
SyntaxError: invalid syntax
All the Python Keywords are case-sensitive, which means a word in python will act as a keyword if you write it as it is supposed to.
E.g.
Correct
def function_1():
pass
Wrong
DeF function_1(): #it will through an Error
SyntaxError:
invalid syntax
def
def
is also a keyword used in python to declare a function. Instead of
def
if you write
DeF
then it will generate a
Syntax Error: invalid syntax
Exception. You need to write the keywords as they are supposed to write and as they were defined in Python. The Keywords in python are in lowercase except for these three
True, False,
and
None
Python Reserved Keywords
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Python Identifiers or Python variables
In Python, when you declare a class, a function, or a variable, you give it a name that could be anything according to you.
Those names are Identifiers by which you identify your class, functions, or variables. Identifiers are the opposite of the Keywords. Identifiers are not reserved in python. You can give any name to the identifiers you suit, but there are some rules to writing an identifier or variable name. Identifiers are case-sensitive, too e.g.
Note: Python identifiers are also known as Python variables. The term identifers and variables are used interchangeably.
variable = 2
Variable =3
print(variable)
print(Variable)
Output
2
3
In the above example, we have defined two variables, first is
variable
and the 2nd is
Variable
and both are different.
As we have mentioned above, some sets of rules for writing an Identifier exist.
Rules To write an Identifier.
- An identifier can be a combination of alphabets that is lowercase (a to z) or uppercase (A to Z)) digits(0 to 9), or an underscore(_).
- You can not use a digit or unique character except underscore (_) to start an identifier name.
- You cannot use any special symbol to write an Identifier.
- You cannot give blank space to write an identifier.
- You cannot use a reserved keyword to write an identifier.
E.g.
Variable23 = 44 # Valid Identifier
23Variable = 44 #Invalid Identifier start with a digit
_23Variable = 44 #Valid variable start with underscore
Variable 23 = 44 # Invalid blank space between Variable and 23
Variable@ 23 = 44 # Invalid blank it contains a special symbol @
Variable_23 = 44 #Valid there is no space in between the identifier
False = 44 #Invalid Identifier False is a Keyword
Conclusion
Python keywords are reserved words or names which perform some specific task or have a special meaning. Python keywords can not be used as a variable name. On the other hand, if we want to give a name to a value, class, function, or object, we can use the identifiers.
Point to Remember
- Python is case-sensitive, so be careful while using cases.
- The Identifiers could be of any length.
- Always use an underscore when you write a lengthy identifier.